package com.mathassistant.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.MathAssistantMVP;
import com.mathassistant.client.event.ToAdminViewEvent;
import com.mathassistant.client.event.ToAdminViewEventHandler;
import com.mathassistant.client.event.ToUserViewEvent;
import com.mathassistant.client.event.ToUserViewEventHandler;
import com.mathassistant.client.event.exercise.ExerciseAddButtonClickedEvent;
import com.mathassistant.client.event.exercise.ExerciseEditFinishedEvent;
import com.mathassistant.client.event.exercise.ExerciseEditFinishedEventHandler;
import com.mathassistant.client.event.theory.TheoryEditEvent;
import com.mathassistant.client.event.theory.TheoryEditEventHandler;
import com.mathassistant.client.event.theory.TheoryEditFinisdedEvent;
import com.mathassistant.client.event.theory.TheoryEditFinishedEventHandler;
import com.mathassistant.client.event.topic.TopicChoosedEvent;
import com.mathassistant.client.event.topic.TopicChoosedEventHandler;
import com.mathassistant.shared.Constant;
import com.mathassistant.shared.TopicDTO;

public class TitleContentPresenter implements Presenter {

	public interface Display{
		HasClickHandlers getAddExerciseHyperLink();
		
		HasClickHandlers getAddExerciseImage();
		
		void setCurrentTitle(String title);
		
		void setTextAnchor(String text);
		
		void setStateAdmin();
		
		void setStateNormal();
		
		Widget asWidget();
	}
	
	private final Display display;
	private final SimpleEventBus eventBus;
	private TopicDTO topicDTO;
	
	public TitleContentPresenter(TopicDTO topicDTO, SimpleEventBus eventBus, Display view){
		this.topicDTO = topicDTO;
		this.eventBus = eventBus;
		this.display = view;
		bind();
	}
	
	private void bind(){
		display.getAddExerciseHyperLink().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				doAddExercise();
			}
		});
		display.getAddExerciseImage().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				doAddExercise();
			}
		});
		eventBus.addHandler(TopicChoosedEvent.TYPE, new TopicChoosedEventHandler() {
			
			@Override
			public void onTopicChoosed(TopicChoosedEvent event) {
				// TODO Auto-generated method stub
				doItemTopicSelected(event);
			}
		});
		eventBus.addHandler(ToAdminViewEvent.TYPE, new ToAdminViewEventHandler() {
			
			@Override
			public void onTopicListToAdminView(ToAdminViewEvent event) {
				// TODO Auto-generated method stub
				if(topicDTO != null){
					if(topicDTO.getContentType().equals(Constant.STEP_EXERCISE)||
							topicDTO.getContentType().equals(Constant.TEST_EXERCISE)){
						display.setTextAnchor("Thêm bài tập");
					} else if (topicDTO.getContentType().equals(Constant.THEORY)){
						display.setTextAnchor("Thêm bài học");
					}
					display.setStateAdmin();
				}
			}
		});
		eventBus.addHandler(ToUserViewEvent.TYPE, new ToUserViewEventHandler() {
			
			@Override
			public void onTopicListToUserView(ToUserViewEvent event) {
				// TODO Auto-generated method stub
				display.setStateNormal();
			}
		});
		eventBus.addHandler(ExerciseEditFinishedEvent.TYPE, new ExerciseEditFinishedEventHandler() {
			
			@Override
			public void onEditExercise(ExerciseEditFinishedEvent event) {
				// TODO Auto-generated method stub
				if(topicDTO != null)
					display.setStateAdmin();
			}
		});
		eventBus.addHandler(TheoryEditFinisdedEvent.TYPE, new TheoryEditFinishedEventHandler() {
			
			@Override
			public void onEditTheoryFinished(TheoryEditFinisdedEvent event) {
				// TODO Auto-generated method stub
				if(topicDTO != null)
					display.setStateAdmin();
			}
		});
		eventBus.addHandler(TheoryEditEvent.TYPE, new TheoryEditEventHandler() {
			
			@Override
			public void onEditTheory(TheoryEditEvent event) {
				// TODO Auto-generated method stub
				display.setStateNormal();
			}
		});
	}
	
	protected void doItemTopicSelected(TopicChoosedEvent event) {
		// TODO Auto-generated method stub
		topicDTO = event.getTopic();
		if(topicDTO != null && MathAssistantMVP.get().getIsLogin()){
			display.setStateAdmin();
			if(event.getTopic().getContentType().equals(Constant.STEP_EXERCISE) || 
					event.getTopic().getContentType().equals(Constant.TEST_EXERCISE)){
				display.setTextAnchor("Thêm bài tập");
			} else if (event.getTopic().getContentType().equals(Constant.THEORY)){
				display.setTextAnchor("Thêm bài học");
			}
		}
		display.setCurrentTitle(event.getTopic().getContent());
	}

	protected void doAddExercise() {
		// TODO Auto-generated method stub
		if(topicDTO != null){
			display.setStateNormal();
//			eventBus.fireEvent(new ExerciseAddEvent(topicDTO));
			eventBus.fireEvent(new ExerciseAddButtonClickedEvent());
		}
	}

	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public void go() {
		// TODO Auto-generated method stub
	}

}
